﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BUS
{
    public class XuLyPhong
    {
        // lay thong tin 1 phong theo ma phong
        public static DTO.CPHONG TimPhongTheoMaPhong(String MaP)
        {
            DTO.CPHONG p = new DTO.CPHONG();
            // lop DataProvider
            DAO.CDataProvider reader = new DAO.CDataProvider();
            // chuoi SELECT
            String sql = " SELECT * FROM PHONG " +
            " WHERE MaPhong = '" + MaP.ToString() + "' ";

            try
            {
                // Bang data se duoc nhap
                DataTable dt = reader.DocDuLieu(sql);

                // Neu ton tai phong nay
                if (dt.Rows.Count == 1)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        p.TenPhong = dr["TenPhong"].ToString();
                        p.MaPhong = dr["MaPhong"].ToString();
                        p.MaLoaiPhong = dr["MaLoaiPhong"].ToString();
                        p.TinhTrang = int.Parse(dr["TinhTrang"].ToString());
                    }
                }
                // phong nay ko ton tai
                else
                {
                    p.TenPhong = "khong tim thay";
                    p.MaPhong = "";
                    p.TinhTrang = 0;
                    p.MaLoaiPhong = "";
                }
            }
            catch (System.Exception )
            {
            	
            }
            

            return p;
        }

        // tim danh sach  toan bo phong
        public static DataTable TimDSPhong()
        {
            // lop DataProvider
            DAO.CDataProvider reader = new DAO.CDataProvider();
            // chuoi SELECT
            String sql = " SELECT * FROM PHONG ";

            DataTable dt = new DataTable();
            try
            {
                // Bang data se duoc nhap
                dt = reader.DocDuLieu(sql);


            }
            catch (System.Exception)
            {

            }


            return dt;
        }
        // tim danh sach cac phong con trong
        public static DataTable TimDSPhongTrong()
        {
            // lop DataProvider
            DAO.CDataProvider reader = new DAO.CDataProvider();
            // chuoi SELECT
            String sql = " SELECT * FROM PHONG " +
            " WHERE TinhTrang = " + 0 + " ";

            DataTable dt = new DataTable();
            try
            {
                // Bang data se duoc nhap
                dt = reader.DocDuLieu(sql);

                
            }
            catch (System.Exception )
            {
            	
            }
            

            return dt;
        }
        // them 1 phong
        public static Boolean ThemPhong(DTO.CPHONG p)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String insertSQL = " INSERT INTO PHONG(TenPhong,MaPhong,MaLoaiPhong,TinhTrang) "
                + " VALUES('" + p.TenPhong.ToString() + "',"
                + "'" + p.MaPhong.ToString() + "',"
                + "'" + p.MaLoaiPhong.ToString() + "',"
                + p.TinhTrang + ") ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(insertSQL))
                return true;
            else
                return false;
        }
        // sua thong tin 1 phong
        public static Boolean SuaTTPhong(DTO.CPHONG p, String MaP)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String updateSQL = " UPDATE PHONG "
                + " SET TenPhong = '" + p.TenPhong.ToString() + "', "
                 + " MaLoaiPhong = '" + p.MaLoaiPhong.ToString() + "',"
                + " TinhTrang = " + p.TinhTrang.ToString() + " "
                + " WHERE MaPhong ='" + MaP.ToString() + "' ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(updateSQL))
                return true;
            else
                return false;
        }
        // sua tinh trang 1 phong
        public static Boolean SuaTinhTrangPhong(int pTT, String MaP)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String updateSQL = " UPDATE PHONG "
                + " SET TinhTrang = " + pTT + " WHERE MaPhong ='" + MaP.ToString() + "' ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(updateSQL))
                return true;
            else
                return false;
        }
        // xoa 1 phong
        public static Boolean XoaPhong(String MaP)
        {
            DAO.CDataProvider writer = new DAO.CDataProvider();

            // Cau lenh SQL
            String deleteSQL = " DELETE FROM PHONG "
                + " WHERE MaPhong ='" + MaP + "' ";

            // Thuc thi cau lenh insert
            if (writer.ThucThiDuLieu(deleteSQL))
                return true;
            else
                return false;
        }
    }
}
